package com.education.util;


import javax.imageio.ImageIO;
import javax.imageio.ImageReader;
import javax.imageio.stream.FileImageInputStream;
import javax.imageio.stream.ImageInputStream;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.Iterator;

/**
 * 读取大图片的分辨率
 *
 * @author 远方bruce
 *
 */
public class ReadResolution {
    /**
     * 通过BufferedImage获取
     * @param file 文件
     * @return 图片的分辨率
     * @throws IOException
     */
    public static String getResolution1(File file) throws IOException {
        BufferedImage image = ImageIO.read(file);
        return image.getWidth() + "x" + image.getHeight();
    }

    /**
     * 获取图片的分辨率
     *
     * @param path
     * @return
     */
    public static Dimension getImageDim(String path) {
        Dimension result = null;
        String suffix = getFileSuffix(path);
        //解码具有给定后缀的文件
        Iterator<ImageReader> iter = ImageIO.getImageReadersBySuffix(suffix);
        System.out.println(ImageIO.getImageReadersBySuffix(suffix));
        if (iter.hasNext()) {
            ImageReader reader = iter.next();
            try {
                ImageInputStream stream = new FileImageInputStream(new File(
                        path));
                reader.setInput(stream);
                int width = reader.getWidth(reader.getMinIndex());
                int height = reader.getHeight(reader.getMinIndex());

                result = new Dimension(width, height);
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                reader.dispose();
            }
        }
        System.out.println("getImageDim:" + result);
        return result;
    }


    public static Dimension getImageDimExt(String path,String suffix) {
        Dimension result = null;
        //  String suffix = getFileSuffix(path);
        //解码具有给定后缀的文件
        Iterator<ImageReader> iter = ImageIO.getImageReadersBySuffix(suffix);
        System.out.println(ImageIO.getImageReadersBySuffix(suffix));
        if (iter.hasNext()) {
            ImageReader reader = iter.next();
            try {
                ImageInputStream stream = new FileImageInputStream(new File(
                        path));
                reader.setInput(stream);
                int width = reader.getWidth(reader.getMinIndex());
                int height = reader.getHeight(reader.getMinIndex());

                result = new Dimension(width, height);
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                reader.dispose();
            }
        }
        System.out.println("getImageDim:" + result);
        return result;
    }



    /**
     * 获得图片的后缀名
     * @param path
     * @return
     */
    private static String getFileSuffix(final String path) {
        String result = null;
        if (path != null) {
            result = "";
            if (path.lastIndexOf('.') != -1) {
                result = path.substring(path.lastIndexOf('.'));
                if (result.startsWith(".")) {
                    result = result.substring(1);
                }
            }
        }
        System.out.println("getFileSuffix:" + result);
        return result;
    }

    /**
     * 截取Dimension对象获得分辨率
     * @param path
     *
     * @return
     */
    public static String getResolution2(String path) {
        String s = getImageDim(path).toString();
        s = s.substring(s.indexOf("[") + 1, s.indexOf("]"));
        String w = s.substring(s.indexOf("=") + 1, s.indexOf(","));
        String h = s.substring(s.lastIndexOf("=") + 1);
        String result = w + " x " + h;
        System.out.println("getResolution:" + result);
        return result;
    }


    public static ImgRate getResolution3(String path) {
        ImgRate p=new ImgRate();
        p.w=0;
        p.h=0;
        p.info="";
        Dimension dd=getImageDim(path);
        if(dd==null){
            dd=getImageDimExt(path,"png");
            if(dd==null){
                dd=getImageDimExt(path,"jpg");

            }
            if(dd==null){
                p.info="无法获取，图片格式异常！！！";
                return p;
            }

        }
        String s = dd.toString();
        s = s.substring(s.indexOf("[") + 1, s.indexOf("]"));
        String w = s.substring(s.indexOf("=") + 1, s.indexOf(","));
        String h = s.substring(s.lastIndexOf("=") + 1);
        String result = w + " x " + h;
        System.out.println("getResolution:" + result);

        p.w=Integer.parseInt(w);
        p.h=Integer.parseInt(h);
        p.info=result;
        return p;
    }


    public static long getFileSize(String path){
        File fx = new File(path);
        return fx.length()/1024;
    }

    /**
     * 测试
     *
     * @param args
     * @throws IOException
     */
    public static void main222(String[] args) throws IOException {
        String path = "d:\\abc.JPG";
        File file = new File(path);
        System.out.println("第1种方法使用的时间：");
        long s1 = System.currentTimeMillis();
        System.out.println(getResolution1(file));
        long l1 = System.currentTimeMillis();
        System.out.println((l1 - s1)+"ms");
        System.out.println("******************************");
        System.out.println("第2种方法使用的时间：");
        long s2 = System.currentTimeMillis();
        getResolution2(path);
        long l2 = System.currentTimeMillis();
        System.out.println((l2 - s2)+"ms");
    }
}
